package cn.manyufun.myf.ui.main.model;


import java.util.ArrayList;
import java.util.List;

import cn.manyufun.myf.app.AppApplication;
import cn.manyufun.myf.app.AppConstant;
import cn.manyufun.myf.bean.NewsChannelTable;
import cn.manyufun.myf.common.baserx.RxSchedulers;
import cn.manyufun.myf.common.commonutils.ACache;
import cn.manyufun.myf.db.NewsChannelTableManager;
import cn.manyufun.myf.ui.main.contract.NewsMainContract;
import rx.Observable;
import rx.Subscriber;

/**
 * @author Kevin Xiao
 * @date Create Date: 2017-12-26
 * @Description
 */
public class NewsMainModel implements NewsMainContract.Model {
    @Override
    public Observable<List<NewsChannelTable>> lodeMineNewsChannels() {

        return Observable.create(new Observable.OnSubscribe<List<NewsChannelTable>>() {
            @Override
            public void call(Subscriber<? super List<NewsChannelTable>> subscriber) {
                ArrayList<NewsChannelTable> newsChannelTableList =
                        (ArrayList<NewsChannelTable>) ACache.get(AppApplication.getAppContext()).getAsObject(AppConstant.CHANNEL_MINE);
                if (newsChannelTableList == null) {
                    newsChannelTableList = (ArrayList<NewsChannelTable>) NewsChannelTableManager.loadNewsChannelsStatic();
                    ACache.get(AppApplication.getAppContext()).put(AppConstant.CHANNEL_MINE, newsChannelTableList);
                }
                subscriber.onNext(newsChannelTableList);
                subscriber.onCompleted();
            }
        }).compose(RxSchedulers.<List<NewsChannelTable>>io_main());
    }
}
